package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 发布工厂
 */
public class PostFactoryActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rb_post_factory_tax_yes)
    RadioButton rbPostFactoryTaxYes;
    @Bind(R.id.rb_post_factory_tax_no)
    RadioButton rbPostFactoryTaxNo;
    @Bind(R.id.rg_post_factory_tax)
    RadioGroup rgPostFactoryTax;
    @Bind(R.id.tv_post_factory_start_time)
    TextView tvPostFactoryStartTime;
    @Bind(R.id.ll_post_factory_start_time)
    LinearLayout llPostFactoryStartTime;
    @Bind(R.id.et_post_factory_free_days)
    EditText etPostFactoryFreeDays;
    @Bind(R.id.et_post_factory_number)
    EditText etPostFactoryNumber;
    @Bind(R.id.et_post_factory_fabric)
    EditText etPostFactoryFabric;
    @Bind(R.id.et_post_factory_style)
    EditText etPostFactoryStyle;
    @Bind(R.id.tv_post_factory_procedure)
    TextView tvPostFactoryProcedure;
    @Bind(R.id.ll_post_factory_procedure)
    LinearLayout llPostFactoryProcedure;
    @Bind(R.id.et_post_factory_invioce_rate)
    EditText etPostFactoryInvioceRate;
    @Bind(R.id.ll_post_factory_choose_photo)
    LinearLayout llPostFactoryChoosePhoto;
    @Bind(R.id.tv_post_factory_commit)
    TextView tvPostFactoryCommit;
    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;
    private boolean isTax=true;
    private String tax="1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_factory);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_factory, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }
    private void initEvent() {
        rbPostFactoryTaxYes.setChecked(true);
        rgPostFactoryTax.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.rb_post_factory_tax_yes:
                        isTax=true;
                        tax="1";
                        break;
                    case R.id.rb_post_factory_tax_no:
                        isTax=false;
                        tax="0";
                        break;
                }
            }
        });
        llPostFactoryChoosePhoto.setOnClickListener(this);
        llPostFactoryProcedure.setOnClickListener(this);
        llPostFactoryStartTime.setOnClickListener(this);
        tvPostFactoryCommit.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_factory_choose_photo:
                Intent intent=new Intent(this,ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.ll_post_factory_start_time:

                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostFactoryStartTime);
                break;
            case R.id.ll_post_factory_procedure:
                final List<String> procedures=new ArrayList<>();
                String [] procedure={"裁缝","车缝","后整理","其他"};
                for (int i=0;i<procedure.length;i++)
                {
                    procedures.add(procedure[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, procedures), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFactoryProcedure.setText(procedures.get(position));
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_post_factory_commit:
                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    String startTime = tvPostFactoryStartTime.getText().toString();
                    String freeDays=etPostFactoryFreeDays.getText().toString();
                    String number = etPostFactoryNumber.getText().toString();
                    String fabric = etPostFactoryFabric.getText().toString();
                    String style = etPostFactoryStyle.getText().toString();
                    String procedurename = tvPostFactoryProcedure.getText().toString();
                    String invioce_rate = etPostFactoryInvioceRate.getText().toString();
                    if (Utils.isEmpty(startTime)) {
                        T.showShort(this, "请选择可上车位时间");
                    } else if (Utils.isEmpty(freeDays)) {
                        T.showShort(this, "请输入空挡天数");
                    } else if (Utils.isEmpty(number)) {
                        T.showShort(this, "请输入可安排车位人数");
                    } else if (Utils.isEmpty(fabric)) {
                        T.showShort(this, "请输入擅长面料");
                    } else if (Utils.isEmpty(style)) {
                        T.showShort(this, "请输入擅长款式");
                    } else if (Utils.isEmpty(procedurename)) {
                        T.showShort(this, "请选择可做工序");
                    }else if(isTax){
                        if(Utils.isEmpty(invioce_rate)){
                            T.showShort(this, "请输入开票税率");
                        }
                    }else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }

    private void commit() {
        String startTime = TimeUtils.dataOne(tvPostFactoryStartTime.getText().toString());
        //有效期
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POSTFACTORY, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(PostFactoryActivity.this,e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(PostFactoryActivity.this, response);
                }
            },files,fileKeys,getParams(startTime,etPostFactoryFreeDays.getText().toString(),etPostFactoryNumber.getText().toString(),
                    etPostFactoryFabric.getText().toString(),etPostFactoryStyle.getText().toString(),tvPostFactoryProcedure.getText().toString(),
                    tax,etPostFactoryInvioceRate.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public OkHttpClientManager.Param[] getParams(String start_time,String free_days, String number, String fabric, String style, String procedure,
                                                 String invioce, String invioce_rate){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this,"id")),
                new OkHttpClientManager.Param("start_time", start_time),
                new OkHttpClientManager.Param("free_days", free_days),
                new OkHttpClientManager.Param("number", number),
                new OkHttpClientManager.Param("fabric", fabric),
                new OkHttpClientManager.Param("style", style),
                new OkHttpClientManager.Param("procedure", procedure),
                new OkHttpClientManager.Param("invioce", invioce),
                new OkHttpClientManager.Param("invioce_rate", invioce_rate)};
    }
}
